<?php
// 用户管理
namespace Admin\Controller;
use Think\Controller;
class UserController extends CommonController {
	//查看用户
	public function viewUser(){
		$db = M('user')->select();
		$this->result =$db;
		$this->display();
	}

	//管理员列表
	public function viewAdmin(){
		$db = M('admin')->select();
		$this->result = $db;
		$this->display();
	}	
	//添加管理员
	public function addAdmin(){
		$this->display();
	}
	//添加用户处理表单
	public function addAdminHandle(){
		$username = I("username");
		$user = M('user')->where(array("username"=>$username))->select();
		if($user){
			$result = M('admin')->where(array("name"=>$username))->select();
			if($result){
				$this->error("管理员已存在");
			}else{
				$data['name'] =$username;
				if(M('admin')->add($data)){
					$this->success("管理员添加成功");
				}else{
					$this->error("管理员添加失败");
				}
				
			}
		}else{
			$this->error("用户不存在");
		}
	}
	//删除管理员
	public function delAdminHandle(){
		$name = I("name");
		if($name == "admin"){
			$this->error("默认管理员不得删除");
		}else{
			$result = M('admin')->where(array("name"=>$name))->delete();
			if($result){
				$this->success("删除成功");
			}else{
				$this->error("删除失败");	
			}
			
		}
	}	
	//锁定用户
	public function lockUser(){
		$this->display();
	}
	//锁定用户
	public function lockUserHandle(){
		$db = M("user");
		$data['lock'] =1;
		$result = $db->where(array("username"=>I("username")))->save($data);
		if($result){
			$this->success("用户锁定成功");
		}else{
			$this->error("用户锁定失败");
		}
	}	
	//解锁用户
	public function unlockUser(){
		$this->display();
	}

	//解锁用户表单
	public function unlockUserHandle(){
		$db = M("user");
		$data['lock'] =0;
		$result = $db->where(array("username"=>I("username")))->save($data);
		if($result){
			$this->success("用户解锁成功");
		}else{
			$this->error("用户解锁失败");
		}		
	}
}